package com.ruoyi.individual.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.book.domain.Book;
import com.ruoyi.individual.enumeration.StateEnum;
import com.ruoyi.individual.enumeration.TypeEnum;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.individual.service.IBookPrivateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 共享图书Controller
 * 
 * @author yaohui
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/individual/share")
public class BookPrivateController extends BaseController {
    @Autowired
    private IBookPrivateService bookPrivateService;

    /**
     * 查询共享图书列表
     */
    @PreAuthorize("@ss.hasPermi('individual:share:list')")
    @GetMapping("/list")
    public TableDataInfo list(Book bookPrivate) {
        startPage();
        String username = getUsername();
        List<Book> list = bookPrivateService.selectBookPrivateList(bookPrivate , username);
        return getDataTable(list);
    }

    /**
     * 导出共享图书列表
     */
    @PreAuthorize("@ss.hasPermi('individual:share:export')")
    @Log(title = "共享图书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Book bookPrivate) {
        String username = getUsername();
        List<Book> list = bookPrivateService.selectBookPrivateList(bookPrivate, username);
        for (Book aPrivate : list) {
            aPrivate.setState(Objects.requireNonNull(StateEnum.getStateByCode(aPrivate.getState())).getName());
            aPrivate.setType(Objects.requireNonNull(TypeEnum.getStateByCode(aPrivate.getType())).getName());
        }
        ExcelUtil<Book> util = new ExcelUtil<>(Book.class);
        util.exportExcel(response, list, "个人共享图书数据");
    }

    /**
     * 获取共享图书详细信息
     */
    @PreAuthorize("@ss.hasPermi('individual:share:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bookPrivateService.selectBookPrivateById(id));
    }

    /**
     * 新增共享图书
     */
    @PreAuthorize("@ss.hasPermi('individual:share:add')")
    @Log(title = "共享图书", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Book bookPrivate) {
        bookPrivate.setCreateBy(getUsername());
        return toAjax(bookPrivateService.insertBookPrivate(bookPrivate));
    }

    /**
     * 修改共享图书
     */
    @PreAuthorize("@ss.hasPermi('individual:share:edit')")
    @Log(title = "共享图书", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Book bookPrivate) {
        return toAjax(bookPrivateService.updateBookPrivate(bookPrivate));
    }

    /**
     * 删除共享图书
     */
    @PreAuthorize("@ss.hasPermi('individual:share:remove')")
    @Log(title = "共享图书", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bookPrivateService.deleteBookPrivateByIds(ids));
    }
}
